package com.tuiyun.jxgl.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


import com.tuiyun.jxgl.bean.Driver;
import com.tuiyun.jxgl.bean.Msgtemplate;
import com.tuiyun.jxgl.bean.Student;

public class MSGService {
	
	private String bizcode;
	
	private String admin_user;
	
	private String admin_pwd;
		
	public static void main(String args[]){
		MSGService mg = new MSGService();
		mg.setAdmin_pwd("aa123456");
		mg.setAdmin_user("admin");
		mg.setBizcode("57177546974");
		mg._send("测试中文", "18621636416");
	}
	
	private void _send(String content,String receiver){
		//login to wap2
		//57177546974
		//admin aa123456
		HttpClient httpclient = new DefaultHttpClient();
		HttpClientParams.setCookiePolicy(httpclient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);        
		HttpGet httpget = new HttpGet("http://www.zjsmap.com/smap/wap2");
        HttpResponse response;
		try {
			response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();

	        System.out.println("Login form get: " + response.getStatusLine());
	        EntityUtils.consume(entity);
	        System.out.println("Initial set of cookies:");
	        List<Cookie> cookies = ((AbstractHttpClient) httpclient).getCookieStore().getCookies();        
	        if (cookies.isEmpty()) {
	            System.out.println("None cookie");
	        } else {
	            for (int i = 0; i < cookies.size(); i++) {
	                System.out.println("- " + cookies.get(i).toString());
	            }
	        }
	        
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			
			nameValuePairs.add(new BasicNameValuePair("flag","smapLogin"));
			nameValuePairs.add(new BasicNameValuePair("ECCODE",this.getBizcode()));
			nameValuePairs.add(new BasicNameValuePair("MEMBERACCOUNT",this.getAdmin_user()));
			nameValuePairs.add(new BasicNameValuePair("PASSWORD",this.getAdmin_pwd()));
			
			
			HttpPost hp = new HttpPost("http://www.zjsmap.com/smap/wap2/login.do?method=login");
			hp.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
			response = httpclient.execute(hp);
			entity = response.getEntity();
	        System.out.println("Login form get: " + response.getStatusLine());
	        EntityUtils.consume(entity);	        
	        nameValuePairs = new ArrayList<NameValuePair>();			
			nameValuePairs.add(new BasicNameValuePair("OPT","SEND"));
			nameValuePairs.add(new BasicNameValuePair("msg","1"));
			nameValuePairs.add(new BasicNameValuePair("destId",receiver));
			nameValuePairs.add(new BasicNameValuePair("msgContent",content));		
			hp = new HttpPost("http://www.zjsmap.com/smap/wap2/sms.do");
			hp.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
			response = httpclient.execute(hp);
			entity = response.getEntity();
	        System.out.println("Login form get: " + response.getStatusLine());
	        EntityUtils.consume(entity);

		} catch (ClientProtocolException e1) {
			
			e1.printStackTrace();
		} catch (IOException e1) {			
			e1.printStackTrace();
		}
           

        
	}
	
	public void sendMsg(Driver d,Msgtemplate m,Student s){
		String template = m.getText();
		template = template.replaceAll(":name", d.getName());
		template = template.replaceAll(":stuname", s.getRealName());		
		template = template.replaceAll(":stuphone", s.getPhone());
		final String s1 = new String(template);
		final String s2 = new String(d.getPhone());
		new Thread(new Runnable(){
			@Override
			public void run() {
				_send(s1,s2);				
			}}
		).start();		
	}	
	
	public String getBizcode() {
		return bizcode;
	}

	public void setBizcode(String bizcode) {
		this.bizcode = bizcode;
	}

	public String getAdmin_user() {
		return admin_user;
	}

	public void setAdmin_user(String admin_user) {
		this.admin_user = admin_user;
	}

	public String getAdmin_pwd() {
		return admin_pwd;
	}

	public void setAdmin_pwd(String admin_pwd) {
		this.admin_pwd = admin_pwd;
	}

}
